package com.tengju.bff.interfaces.covercharge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.tengju.bff.interfaces.covercharge.constant.OnlinePayMoneyErrorNum;
import com.tengju.bff.interfaces.covercharge.constant.ServiceWithdarwCycleAddTransOrderEnum;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceWithdarwCycleAddTransOrder;
import com.tengju.bff.interfaces.covercharge.mapper.ServiceWithdarwCycleAddTransOrderMapper;
import com.tengju.bff.interfaces.covercharge.service.IServiceWithdarwCycleAddTransOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author cj
 * @date 2020/10/27
 * @description 服务费周期交易表 实现类
 */
@Service
public class ServiceWithdarwCycleAddTransOrderServiceImpl extends ServiceImpl<ServiceWithdarwCycleAddTransOrderMapper, ServiceWithdarwCycleAddTransOrder> implements IServiceWithdarwCycleAddTransOrderService {

    @Autowired
    private ServiceWithdarwCycleAddTransOrderMapper serviceWithdarwCycleAddTransOrderMapper;

    /**
     * 生成订单号
     * @param
     * @return String
     * */
    @Override
    public String createBusinessNo(){
        //UUID uuid = UUID.randomUUID();生成uuid
        //最大支持1-9个集群机器部署
        int machineId = 1;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        //有可能是负数
        if(hashCodeV < 0) {
            hashCodeV = - hashCodeV;
        }
        // 0 代表前面补充0
        // 15 代表长度为15
        // d 代表参数为正数型
        return  machineId+ String.format("%015d", hashCodeV);
    }

    /**
     * 获取列表
     * @param queryWrapper
     * @return
     * */
    @Override
    public Page<ServiceWithdarwCycleAddTransOrder> getPageListByQuery(Page<ServiceWithdarwCycleAddTransOrder> page, QueryWrapper<ServiceWithdarwCycleAddTransOrder> queryWrapper) {
        return serviceWithdarwCycleAddTransOrderMapper.selectPage(page,queryWrapper);
    }

    /**
     * 更新
     * @param id id
     * @param num 失败次数
     * @param status 状态
     * @param summary 备注
     * @return
     * */
    @Override
    public void updateByQuery(Integer id,Integer num,Integer status,String summary){
        ServiceWithdarwCycleAddTransOrder withdarwCycleAddTransOrderUpdate=new ServiceWithdarwCycleAddTransOrder();
        withdarwCycleAddTransOrderUpdate.setId(id);
        withdarwCycleAddTransOrderUpdate.setStatus(status);
        withdarwCycleAddTransOrderUpdate.setNum(num);
        withdarwCycleAddTransOrderUpdate.setUpdatedTime(new Date());
        withdarwCycleAddTransOrderUpdate.setSummary(summary);
        this.updateById(withdarwCycleAddTransOrderUpdate);
    }

    /**
     * 获取未知的状态数
     * @param cycleAddId 周期id
     * @return
     * */
    @Override
    public ServiceWithdarwCycleAddTransOrder getUnknown(Integer cycleAddId){
        QueryWrapper<ServiceWithdarwCycleAddTransOrder> cycleAddTransOrderQueryWrapper = new QueryWrapper<>();
        cycleAddTransOrderQueryWrapper.eq("cycle_add_id", cycleAddId);
        cycleAddTransOrderQueryWrapper.eq( "status", ServiceWithdarwCycleAddTransOrderEnum.UNKNOWN.getCode());
        cycleAddTransOrderQueryWrapper.ge("num",OnlinePayMoneyErrorNum.SERVICE_WITHDARE_CYCLE_ADD_TRANS);
        return this.baseMapper.selectOne(cycleAddTransOrderQueryWrapper);
    }

    /**
     * 获取交易表为成功状态
     * @param cycleAddId 周期id
     * @return
     * */
    @Override
    public ServiceWithdarwCycleAddTransOrder getSuccessful(Integer cycleAddId){
        QueryWrapper<ServiceWithdarwCycleAddTransOrder> cycleAddSuccessfulQueryWrapper = new QueryWrapper<>();
        cycleAddSuccessfulQueryWrapper.eq("cycle_add_id", cycleAddId);
        cycleAddSuccessfulQueryWrapper.eq( "status", ServiceWithdarwCycleAddTransOrderEnum.SUCCESSFUL.getCode());
        return this.baseMapper.selectOne(cycleAddSuccessfulQueryWrapper);
    }

    /**
     * 获取交易表为成功状态
     * @param cycleAddId 周期id
     * @param isHasNum 是否加数字校验
     * @return
     * */
    @Override
    public ServiceWithdarwCycleAddTransOrder getProcessOrUnKnow(Integer cycleAddId,Boolean isHasNum){
        List<Integer> statusList = Lists.newArrayList();
        statusList.add(ServiceWithdarwCycleAddTransOrderEnum.DEALING.getCode());
        statusList.add(ServiceWithdarwCycleAddTransOrderEnum.UNKNOWN.getCode());
        QueryWrapper<ServiceWithdarwCycleAddTransOrder> cycleAddTransOrderQueryWrapper = new QueryWrapper<>();
        cycleAddTransOrderQueryWrapper.eq("cycle_add_id", cycleAddId);
        cycleAddTransOrderQueryWrapper.in( "status", statusList);
        if(isHasNum){
            cycleAddTransOrderQueryWrapper.ge("num",OnlinePayMoneyErrorNum.SERVICE_WITHDARE_CYCLE_ADD_TRANS);
        }
        return this.baseMapper.selectOne(cycleAddTransOrderQueryWrapper);
    }


    @Override
    public ServiceWithdarwCycleAddTransOrder getFail(Integer cycleAddId){
        List<Integer> statusList = Lists.newArrayList();
        statusList.add(ServiceWithdarwCycleAddTransOrderEnum.FAILURE.getCode());
        QueryWrapper<ServiceWithdarwCycleAddTransOrder> cycleAddTransOrderQueryWrapper = new QueryWrapper<>();
        cycleAddTransOrderQueryWrapper.eq("cycle_add_id", cycleAddId);
        cycleAddTransOrderQueryWrapper.in( "status", statusList);
        cycleAddTransOrderQueryWrapper.orderByDesc("created_time");
        cycleAddTransOrderQueryWrapper.last("limit 1");
        return this.baseMapper.selectOne(cycleAddTransOrderQueryWrapper);
    }

    /**
     * 获取交易表为正在处理中和未知的和成功的
     * @param cycleAddId 周期id
     * @return
     * */
    @Override
    public Integer getNotFailNum(Integer cycleAddId){
        List<Integer> statusList = Lists.newArrayList();
        statusList.add(ServiceWithdarwCycleAddTransOrderEnum.DEALING.getCode());
        statusList.add(ServiceWithdarwCycleAddTransOrderEnum.UNKNOWN.getCode());
        statusList.add(ServiceWithdarwCycleAddTransOrderEnum.SUCCESSFUL.getCode());
        QueryWrapper<ServiceWithdarwCycleAddTransOrder> cycleAddTransOrderQueryWrapper = new QueryWrapper<>();
        cycleAddTransOrderQueryWrapper.eq("cycle_add_id", cycleAddId);
        cycleAddTransOrderQueryWrapper.in( "status", statusList);
        return this.baseMapper.selectCount(cycleAddTransOrderQueryWrapper);
    }

    @Override
    public ServiceWithdarwCycleAddTransOrder getByBusinessNo(String businessNo) {
        QueryWrapper<ServiceWithdarwCycleAddTransOrder> cycleAddTransOrderQueryWrapper = new QueryWrapper<>();
        cycleAddTransOrderQueryWrapper.eq("business_no", businessNo);
        return this.baseMapper.selectOne(cycleAddTransOrderQueryWrapper);
    }
}
